//返回固定小数点
let toFixed=(e,n=1)=>{
return Number(e.toFixed(n))
}
//社保计算
let sbjs=(sbsx)=>{
  let socialSecurity=toFixed((sbsx*0.102),2)
let medical=toFixed((sbsx*0.02),2)
let endowment=toFixed((sbsx*0.08),2)
let unemployment=toFixed((sbsx*0.002),3)
if(socialSecurity>100)socialSecurity=Math.floor(socialSecurity)
return {
  socialSecurity,
  medical,
  endowment,
  unemployment,
}
}
//公积金计算
let gjjjs=(gjjsx,n)=>{
 let  a=Number(gjjsx)
 let  b=Number(n)
  let gjj=toFixed(a*b)/100,gjjtotal=toFixed(a*b)/100*2
  if(gjjtotal>100)gjjtotal=Math.floor(gjjtotal)
  return {
    gjj,
    gjjtotal,
  }
}
//个税计算
let gsjs=(money,socialSecurity,gjj)=>{//税前工资，社保，公积金
  //阶梯税率
  let m=new Map([
    [3000,0.03],
    [9000,0.1],
    [13000,0.2],
    [10000,0.25],
    [20000,0.3],
    [25000,0.35],
    [Infinity,0.45]
  ])
  //设置最终到手
  let ds=(toFixed((Number(money)-socialSecurity-gjj)))
  let gs=0,d=ds-5000
  if(d>0){
    for(let [x,y] of m){
      let z=x*y
      if(d>x){
        gs+=z
      }else{
        gs+=(d*y)
      }
      d-=x
      if(x==Infinity||d<0)break
    }
    gs=Number((gs).toFixed(2))
    if(gs>100)gs=Math.floor(gs)
    ds-=gs
  }
  ds=toFixed(ds,2)
  return {
    ds,
    gs
  }
}
module.exports = {
toFixed:toFixed,
sbjs:sbjs,
gjjjs:gjjjs,
gsjs:gsjs
}